﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class Assortment :System.Windows.Forms.Form// Form
    {
        public Assortment()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception e)
            {
                string s = e.ToString();
            }
        }
        protected DataSets.Dictionary.AssortmentDataTable Data
        {
            get
            {
                return this.dictionary.Assortment;
            }
        }
        private void Assortment_Load(object sender, EventArgs e)
        {
            try
            {
                lock (this)
                {
                    this.Cursor = Cursors.WaitCursor;
                   // dgvAssortment.DataSource = null;
                    DataSets.DictionaryTableAdapters.AssortmentTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssortmentTableAdapter();
                    adapter.Fill(this.dictionary.Assortment,DIProject.Data.Sync.IsAdmin);
                   // this.assortmentTableAdapter.Fill(this.dictionary.Assortment);                    
                    dgvAssortment.RowCount = this.Data.Count;
                    this.Cursor = Cursors.Default;
                }
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
            }

        }
        /// <summary>
        /// Ищем ИД выделенной строки
        /// </summary>
        private Int32 CurrentRowId
        {
            get
            {
                if (dgvAssortment.SelectedRows.Count < 1)
                {
                    MessageBox.Show("Выбирите строку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                DataRowView drv = dgvAssortment.SelectedRows[0].DataBoundItem as DataRowView;
                DataSets.Dictionary.AssortmentRow ar = drv.Row as DataSets.Dictionary.AssortmentRow;
                return ar.Id;
            }
        }
        private void addButton_Click(object sender, EventArgs e)
        {
            DIProject.Dictionary.AssortmentDetails ad = new AssortmentDetails(Util.ActionType.Insert, this.dictionary.Assortment, Int32.MinValue);
            ad.MdiParent = this.MdiParent;
            ad.Show();
        }

        private void dgvAssortment_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            dgvAssortment.Rows[e.RowIndex].Selected = true;
            DataRowView drv = this.Data.DefaultView[e.RowIndex];// dgvAssortment.SelectedRows[0].DataBoundItem as DataRowView;
            DataSets.Dictionary.AssortmentRow ar = drv.Row as DataSets.Dictionary.AssortmentRow;
            DIProject.Dictionary.AssortmentDetails ad = new AssortmentDetails(Util.ActionType.Update, this.dictionary.Assortment, ar.Id);
            //ad.MdiParent = this.MdiParent;
            ad.Show();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btnFiltr_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbFilter.Text))
            {
                this.Data.DefaultView.RowFilter = string.Empty;
            }
            else
            {
                this.Data.DefaultView.RowFilter = "Name like '*" + txbFilter.Text + "*'";                
            }
            dgvAssortment.Rows.Clear();
            dgvAssortment.RowCount = this.Data.DefaultView.Count;
        }

        private void dgvAssortment_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            if (this.Data.DefaultView.Count < 1)
                return;
            Int32 cInd = e.ColumnIndex;
            Int32 rInd = e.RowIndex;
            string dpn = dgvAssortment.Columns[cInd].DataPropertyName;
            e.Value = this.Data.DefaultView[rInd][dpn];
        }
    }
}